<template>
  <div class="mnt-dialog form-container">
    <el-form ref="form" :model="formModel" label-width="160px" :rules="rules">
      <el-row style="text-align: center">
        <el-col :span="12">
          <el-form-item label="用户登录名:" prop="userid">
            <el-select v-model="formModel.userid" clearable filterable>
              <el-option
                v-for="item in userOptions"
                :key="item.userid"
                :label="item.username"
                :value="item.userid"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="工厂:" prop="tmBasPlantId">
            <el-select
              v-model="formModel.tmBasPlantId"
              clearable
              filterable
              @clear="clearPlant"
              @change="changePlant"
            >
              <el-option
                v-for="item in plantOptions"
                :key="item.tmBasPlantId"
                :label="item.plantNameC"
                :value="item.tmBasPlantId"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="车间:" prop="tmBasWorkshopId">
            <el-select
              v-model="formModel.tmBasWorkshopId"
              clearable
              filterable
              @clear="clearWorkshop"
              @change="changeWorkshop"
            >
              <el-option
                v-for="item in workshopOptions"
                :key="item.tmBasWorkshopId"
                :label="item.workshopNameC"
                :value="item.tmBasWorkshopId"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="工位:" prop="tmBasUlocId">
            <el-select v-model="formModel.tmBasUlocId" clearable filterable>
              <el-option
                v-for="item in ulocOptions"
                :key="item.tmBasUlocId"
                :label="item.ulocName"
                :value="item.tmBasUlocId"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-row class="submit-area">
      <el-col :span="24">
        <el-button @click="closeDialog">取消</el-button>
        <el-button type="primary" @click="handleSubmit()">确定</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>

export default {
  name: 'UserUlocMnt',
  props: ['pageType', 'editData'],
  data() {
    return {
      disabledWorkshop: true,
      disabledUloc: true,
      userOptions: [],
      plantOptions: [],
      workshopOptions: [],
      ulocOptions: [],
      formModel: {
        userUlocId: '',
        userid: '',
        tmBasPlantId: '',
        tmBasWorkshopId: '',
        tmBasUlocId: ''
      },
      rules: {}
    }
  },
  computed: {},
  mounted() {
    this.init()
  },
  methods: {
    init() {
      const param = { status: '1' }
      this.$form = this.$refs.form
      const { pageType, editData, formModel } = this
      if (pageType !== 'add') {
        this.assignData(editData, formModel)
        this.getUserOptions(param)
        this.getPlantOptions(param)
        const paramWorkshop = { status: '1', tmBasPlantId: this.formModel.tmBasPlantId }
        this.getWorkshopOptions(paramWorkshop)
        const paramUloc = { status: '1', tmBasWorkshopId: this.formModel.tmBasWorkshopId }
        this.getUlocOptions(paramUloc)
      } else {
        this.getUserOptions(param)
        this.getPlantOptions(param)
      }
      this.initRules()
    },
    initRules() {
      const REQUIRED_MSG = this.CONST.REQUIRED_MSG
      this.rules = {
        userid: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        tmBasPlantId: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        tmBasWorkshopId: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        tmBasUlocId: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }]
      }
      this.$nextTick(_ => {
        this.$form.clearValidate()
      })
    },
    // 点击后获取工厂对应的车间列表
    changePlant(value) {
      if (value !== '') {
        // 清空车间和工位
        this.formModel.tmBasWorkshopId = ''
        this.formModel.tmBasUlocId = ''
        const param = { tmBasPlantId: value, status: '1' }
        this.get('/tmBasWorkshop/listWorkshop', param).then(res => {
          this.workshopOptions = res
        })
      }
    },
    // 点击后获取车间对应的工位列表
    changeWorkshop(value) {
      if (value !== '') {
        // 清空工位
        this.formModel.tmBasUlocId = ''
        const param = { tmBasWorkshopId: value, status: '1' }
        this.get('/tmBasUloc/listUloc', param).then(res => {
          this.ulocOptions = res
        })
      }
    },
    // 清空工厂
    clearPlant() {
      // 清空车间和工位
      this.formModel.tmBasWorkshopId = ''
      this.formModel.tmBasUlocId = ''
      // 清空车间和工位列表
      this.workshopOptions = ''
      this.ulocOptions = ''
    },
    // 清空车间
    clearWorkshop() {
      // 清空工位
      this.formModel.tmBasUlocId = ''
      // 清空工位列表
      this.ulocOptions = ''
    },
    // 获取用户列表
    getUserOptions(param) {
      this.get('/user/listUser', param).then(res => {
        this.userOptions = res
      })
    },
    // 获取工厂列表
    getPlantOptions(param) {
      this.get('/tmBasPlant/queryTmBasPlantList', param).then(res => {
        this.plantOptions = res
      })
    },
    // 获取车间列表
    getWorkshopOptions(param) {
      this.get('/tmBasWorkshop/listWorkshop', param).then(res => {
        this.workshopOptions = res
      })
    },
    // 获取工位列表
    getUlocOptions(param) {
      this.get('/tmBasUloc/listUloc', param).then(res => {
        this.ulocOptions = res
      })
    },
    // 提交新增或修改
    handleSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.formModel.userUlocId !== '') {
            const tip = '修改成功！'
            const url = '/userUloc/updateUserUloc'
            this.post(url, this.formModel).then(res => {
              this.$info(tip, () => {
                this.$emit('mntSuc', res)
                this.closeDialog()
              })
            })
          } else {
            const url = '/userUloc/saveUserUloc'
            this.post(url, this.formModel).then(res => {
              const tip = '新增成功！'
              this.$info(tip, () => {
                this.$emit('mntSuc', res)
                this.closeDialog()
              })
            })
          }
        }
      })
    },
    closeDialog() {
      this.$parent.$emit('update:visible', false)
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
